package com.sunwayworld.dev.util;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class StringDevUtil {
	private static String format = "yyyy-MM-dd HH:mm";

	public static String getSrcFilePath(String base, String classFullName,
			String fileType) {
		String filePath = base + File.separator;
		filePath += classFullName.replace('.', File.separatorChar) + "."
				+ fileType;
		return filePath;
	}

	public static String toString(Object str) {
		if (str == null) {
			return "";
		} else {
			return str.toString().trim();

		}
	}

	public static String toLowerCase(String str) {
		if (str == null) {
			return "";
		} else {
			return str.toLowerCase();

		}
	}

	public static String toUpperCase(String str) {
		if (str == null) {
			return "";
		} else {
			return str.toUpperCase();
		}
	}

	public static String toUpperFirst(String str) {
		if (str == null) {
			return "";
		} else {
			try {
				String temp = "";
				temp = str.substring(0, 1).toUpperCase();
				temp += str.substring(1, str.length());
				return temp;
			} catch (Exception ex) {
				return "";
			}

		}
	}

	public static String toLowerFirst(String str) {
		if (str == null) {
			return "";
		} else {
			try {
				String temp = "";
				temp = str.substring(0, 1).toLowerCase();
				temp += str.substring(1, str.length());
				return temp;
			} catch (Exception ex) {
				return "";
			}

		}
	}

	public static String safeDate(Object str) {
		if (str == null) {
			return "";
		}
		try {
			if (str instanceof java.util.Date) {
				return formatDate((java.util.Date) str);
			}

			if (str instanceof java.sql.Date) {
				return formatDate((java.sql.Date) str);
			}

			Date d = parseDate(str.toString());

			return formatDate(d);
		} catch (Exception ex) {

			return "";
		}

	}

	public static String formatDate(Date date) {

		if (date == null) {
			return "";
		}
		if (format == null)
			format = "";
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(date);
	}

	private static Date parseDate(String dateStr) {

		SimpleDateFormat sdf = new SimpleDateFormat(format);

		try {
			return sdf.parse(dateStr);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	public static String convertTableName(String tableName) {
		if (tableName == null) {
			return "";
		}
		tableName = tableName.replaceAll("^T_", "");

		String temp[] = tableName.split("_");

		tableName = "";
		for (int i = 0, n = temp.length; i < n; i++) {
			temp[i] = toUpperFirst(toLowerCase(temp[i]));
			tableName += temp[i];
		}

		return tableName;
	}

}
